# -*- coding: utf-8 -*-
"""
TG日期编辑组件 - TgDateEdit
日期选择输入组件
"""
from PySide6.QtWidgets import QDateEdit
from PySide6.QtCore import QDate
from ...style_manager import StyleManager


class TgDateEdit(QDateEdit):
    """TG日期编辑组件"""
    
    def __init__(self, date=None, parent=None):
        super().__init__(parent)
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 设置组件特定属性
        if date is None:
            # 默认设置为当前日期
            self.setDate(QDate.currentDate())
        elif isinstance(date, QDate):
            self.setDate(date)
        else:
            # 如果传入其他格式，尝试转换
            try:
                if hasattr(date, 'year') and hasattr(date, 'month') and hasattr(date, 'day'):
                    # datetime.date 或 datetime.datetime 对象
                    self.setDate(QDate(date.year, date.month, date.day))
                else:
                    self.setDate(QDate.currentDate())
            except:
                self.setDate(QDate.currentDate())
        
        # 设置日期格式为中文格式
        self.setDisplayFormat("yyyy年MM月dd日")
        # 启用日历弹出
        self.setCalendarPopup(True)
        
        # 初始化组件
        self.initialize()
    
    @property
    def component_name(self):
        """获取组件名称"""
        return self._component_name
    
    @property
    def is_initialized(self):
        """检查组件是否已初始化"""
        return self._is_initialized
    
    @property
    def style_applied(self):
        """检查样式是否已应用"""
        return self._style_applied
    
    def setup_ui(self):
        """设置用户界面"""
        # QDateEdit的基本设置已在__init__中完成
        pass
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet(f"""
            QDateEdit {{
                font-size: {StyleManager.FONT_SIZES['normal']};
                padding: {StyleManager.SPACING['sm']};
                border: 1px solid {StyleManager.COLORS['border']};
                border-radius: {StyleManager.BORDER_RADIUS['small']};
                background-color: {StyleManager.COLORS['background']};
                min-width: 120px;
            }}
            QDateEdit:focus {{
                border: 2px solid {StyleManager.COLORS['primary']};
            }}
            QDateEdit::drop-down {{
                subcontrol-origin: padding;
                subcontrol-position: top right;
                width: 20px;
                border-left: 1px solid {StyleManager.COLORS['border']};
                border-top-right-radius: {StyleManager.BORDER_RADIUS['small']};
                border-bottom-right-radius: {StyleManager.BORDER_RADIUS['small']};
                background-color: {StyleManager.COLORS['secondary']};
            }}
            QDateEdit::drop-down:hover {{
                background-color: {StyleManager.COLORS['primary']};
            }}
            QDateEdit::down-arrow {{
                image: none;
                border: 2px solid {StyleManager.COLORS['text_primary']};
                border-top: none;
                border-right: none;
                width: 6px;
                height: 6px;
                margin-top: -2px;
            }}
        """)
        self._style_applied = True
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            self._is_initialized = True
            self._style_applied = True
    
    def get_python_date(self):
        """获取Python日期对象"""
        qdate = self.date()
        return qdate.toPython()
    
    def set_python_date(self, date):
        """设置Python日期对象"""
        if hasattr(date, 'year') and hasattr(date, 'month') and hasattr(date, 'day'):
            self.setDate(QDate(date.year, date.month, date.day))
    
    def get_date_string(self, format_str="yyyy-MM-dd"):
        """获取格式化日期字符串"""
        return self.date().toString(format_str)